package com.hyl.dao;

import java.sql.*;

/**
 * @Description 数据库操作
 * @Author hyl
 * @Date 2022/11/1 15:13
 **/
public class BaseDao {

	/**
	 * 创建数据库连接
	 * @return
	 */
	public Connection getConnection(){
		//1、加载驱动
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		//2、创建连接对象
		Connection conn = null;
		try {
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT-8","root","root");
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		return conn;
	}


	/**
	 * 释放资源
	 * @param conn
	 * @param stm
	 * @param rs
	 */
	public void closeAll(Connection conn, Statement stm, ResultSet rs){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException throwables) {
				throwables.printStackTrace();
			}
		}
		if(stm!=null){
			try {
				stm.close();
			} catch (SQLException throwables) {
				throwables.printStackTrace();
			}
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException throwables) {
				throwables.printStackTrace();
			}
		}

	}


}
